<?php
/**
* 消息数据模型
* @date: 2018年6月5日 上午10:12:44
* @author: xiaocui
*/

namespace app\common\model;

class TagData
{
    /**
    * 获取栏目
    * @date: 2018年8月22日 上午10:00:04
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public static function get_arctype($tid,$field,$limit){
        $list = db('column')
        ->field($field)
        ->where(['pid'=>$tid,'display'=>1])
        ->order('sort asc,id asc')
        ->limit($limit)
        ->select();
        
        foreach ($list as $key=>$val){
            $list[$key]['url'] = rurl(['type'=>$val['type'],'dir'=>$val['dir']],'lists',['id'=>$val['id']]);
        }
        
        return $list;
    }
    
    /**
    * 获取文章列表
    * @date: 2018年8月22日 上午10:00:16
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public static function get_arclist($tid,$field,$order,$limit){
        $col = db('column')->field('id,pid,type,title,dir')->order('id asc')->select();
        import('util.Tree');
        $cids = \Tree::getChildsIds($col,$tid,true);
        $childs = \Tree::getChilds($col,$tid,true);
        
        $dirs = [];
        foreach ($childs as $val){
            $dirs[$val['id']] = $val['dir'];
        }
        
        $tinfo = db('column')->field('id,mid')->find($tid);
        $minfo = db('models')->field('id,name')->find($tinfo['mid']);
        
        if(empty($tinfo) || empty($minfo)) return [];
        
        $map['display'] = 1;
        $map['tid'] = ['in',$cids];
        $list = db($minfo['name'])->field($field)->order($order)->limit($limit)->select();
        foreach ($list as $key=>$val){
            $dir = isset($dirs[$val['tid']])?$dirs[$val['tid']]:'';
            $list[$key]['url'] = rurl(['type'=>10,'dir'=>$dir],'view',['id'=>$val['id']]);
        }
        return $list;
    }
    
    /**
    * 获取广告
    * @date: 2018年8月22日 下午3:28:40
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public static function get_adlist($aid,$tid,$limit){
        if($tid > 0 && $aid==0){
            $adv = db('adv')->where(['tid'=>$tid])->find();
            $aid = intval($adv['id']);
        }
        
        if($tid == 0 && $aid == 0){
            return [];
        }
        
        $map['status'] = 1;
        $map['adv_id'] = $aid;
        $list = db('ads')
        ->field('id,title,description,url,pic')
        ->where($map)->order('sort asc,id asc')
        ->limit($limit)->select();
        return $list;
    }
    
    /**
    * 获取友链
    * @date: 2018年8月22日 下午4:11:51
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public static function get_flink($limit){
        $map['status'] = 1;
        
        $list = db('flink')
        ->field('id,title,url,pic')
        ->where($map)->order('sort asc,id asc')
        ->limit($limit)->select();
        return $list;
    }
}